﻿/*
 * ExtendBy:xzh
 * CreateDate:2010-06-03
 * Description: PageComboResizer
 */
Ext.namespace('Twi.ux');

Twi.ux.PageComboResizer = Ext.extend(Object, {

    pageSizes: [5,10,15, 20, 25, 30, 35, 40, 50, 75, 100, 200],
    prefixText: '每页条数：',
    postfixText: '',

    constructor: function(config)
    {
        Ext.apply(this, config);
        Twi.ux.PageComboResizer.superclass.constructor.call(this, config);
    },

    init: function(pagingToolbar)
    {
        var ps = this.pageSizes;
        var combo = new Ext.form.ComboBox({
            typeAhead: true,
            triggerAction: 'all',
            lazyRender: true,
            mode: 'local',
            width: 45,
            store: ps,
            listeners: {
                select: function(c, r, i) {
                    pagingToolbar.pageSize = ps[i];
                    var rowIndex = 0;
                    var gp = pagingToolbar.findParentBy(
            function(ct, cmp) { return (ct instanceof Ext.grid.GridPanel) ? true : false; }
          );
                    var sm = gp.getSelectionModel();
                    if (undefined != sm && sm.hasSelection())
                    {
                        if (sm instanceof Ext.grid.RowSelectionModel)
                        {
                            rowIndex = gp.store.indexOf(sm.getSelected());
                        } else if (sm instanceof Ext.grid.CellSelectionModel)
                        {
                            rowIndex = sm.getSelectedCell()[0];
                        }
                    }
                    rowIndex += pagingToolbar.cursor;
                    pagingToolbar.doLoad(Math.floor(rowIndex / pagingToolbar.pageSize) * pagingToolbar.pageSize);
                }
            }
        });

        Ext.iterate(this.pageSizes, function(ps)
        {
            if (ps == pagingToolbar.pageSize)
            {
                combo.setValue(ps);
                return;
            }
        });

       // var inputIndex = pagingToolbar.items.indexOf(pagingToolbar.refresh); //放在刷新按钮后面
        var inputIndex =-1; 
        //pagingToolbar.insert(++inputIndex, '-');
        pagingToolbar.insert(++inputIndex, this.prefixText);
        pagingToolbar.insert(++inputIndex, combo);
        pagingToolbar.insert(++inputIndex, this.postfixText);
        pagingToolbar.on({
            beforedestroy: function()
            {
                combo.destroy();
            }
        });

    }
});
